package com.enterpriseDigitalPlatform.controller.renshi.moneyMonth;

import com.enterpriseDigitalPlatform.dto.JsonRes;
import com.enterpriseDigitalPlatform.dto.PageRes;
import com.enterpriseDigitalPlatform.exception.ServiceException;
import com.enterpriseDigitalPlatform.util.RequiredCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/renshi-moneyMonth")
public class MoneyMonthC {
    private MoneyMonthDao moneyMonthDao;

    @Autowired
    public MoneyMonthC(MoneyMonthDao moneyMonthDao) {
        this.moneyMonthDao = moneyMonthDao;
    }

    // 编辑工资记录
    @PostMapping("/editData")
    public Object editData(@RequestBody MoneyMonthDTO moneyMonthDTO) {
        if (RequiredCheckUtil.checkInt(moneyMonthDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        int row = moneyMonthDao.editData(moneyMonthDTO);
        return new JsonRes<>(row);
    }

    // 获取工资记录列表
    @PostMapping("/getList")
    public Object getList(@RequestBody MoneyMonthDTO moneyMonthDTO) {
        if (RequiredCheckUtil.checkInt(moneyMonthDTO.getPage())) {
            moneyMonthDTO.setPage(1);
        }
        if (RequiredCheckUtil.checkInt(moneyMonthDTO.getPageSize())) {
            moneyMonthDTO.setPageSize(20);
        }
        return new JsonRes<>(moneyMonthDao.getList(moneyMonthDTO));
    }

    // 获取工资记录
    @PostMapping("/getData")
    public Object getData(@RequestBody MoneyMonthDTO moneyMonthDTO) {
        if (RequiredCheckUtil.checkInt(moneyMonthDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        PageRes<MoneyMonth> pageRes = moneyMonthDao.getList(moneyMonthDTO);
        MoneyMonth moneyMonth = new MoneyMonth();
        List<MoneyMonth> list = pageRes.getData();
        if (!list.isEmpty()) {
            moneyMonth = list.get(0);
        }
        return new JsonRes<>(moneyMonth);
    }
}
